package com.box.ai.common.tools;

import com.box.ai.common.result.FileInfo;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * minio文件操作工具
 */
@Component
public class MinioUtil {


    @Value("${minio.url}")
    private String url;

    /**
     * 通过依赖注入的方式将声明的minioClient对象以及必要的属性初始化到这个工具类中（minionConfig）
     */
    @Resource
    private MinioClient minioClient;

    /**
     * 创建桶
     */
    public void createBucket(String bucket)throws Exception{
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if (!exists){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
        }
    }

    /**
     * 上传文件
     */
    public void uploadBucket(InputStream inputStream, String bucket,String objectName)throws Exception{
        minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
                .stream(inputStream, -1, 5242889L).build());


    }

    /**
     * 展示所有桶
     */
    public List<String> getAllBucket()throws Exception{
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }

    /**
     * 展示当前桶以及文件
     */
    public List<FileInfo> getBucketAndFile(String bucket)throws Exception{
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucket).build());
        List<FileInfo> fileInfoList = new ArrayList<>();
        for (Result<Item> result : results) {
            FileInfo fileInfo = new FileInfo();
            Item item = result.get();
            fileInfo.setFileName(item.objectName());
            fileInfo.setDirectoryFlag(item.isDir());
            fileInfo.setEtag(item.etag());
            fileInfoList.add(fileInfo);
        }
        return fileInfoList;
    }

    /**
     * 下载
     * @return
     */
    public InputStream downLoad(String bucket,String objectName)throws Exception{
        return minioClient.getObject(
                GetObjectArgs.builder().bucket(bucket).object(objectName).build()
        );
    }

    /**
     * 输出桶
     * @param bucket
     * @throws Exception
     */
    public void deleteBucket(String bucket)throws Exception{
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
    }

    /**
     * 删除文件
     * @param bucket
     * @param objectName
     * @throws Exception
     */
    public void deleteObject(String bucket,String objectName)throws Exception{
        minioClient.removeObject(RemoveObjectArgs.builder().object(objectName).bucket(bucket).build());
    }

    /**
     *获取地址
     * @param bucketName
     * @param objectName
     * @return
     * @throws Exception
     */
    public String getPreviewFileUrl(String bucketName, String objectName) throws Exception{
        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
                .method(Method.GET)
                .bucket(bucketName).object(objectName).build();
        return minioClient.getPresignedObjectUrl(args);
    }


    @SneakyThrows
    public String getUrl(String bucket, String objectName) {

        return url+"/"+bucket+"/"+objectName;
    }
}
